#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
推箱子游戏模型文件
包含游戏元素的数据定义和绘制函数
"""

import pygame
import math

# 游戏元素常量
PATH = 0
WALL = 1
BOX = 2
TARGET = 3
PLAYER = 4
BOX_ON_TARGET = 5
PLAYER_ON_TARGET = 6

# 颜色定义
WALL_COLOR = (100, 100, 100)
BOX_COLOR = (196, 164, 112)
TARGET_COLOR = (255, 255, 0)
PLAYER_COLOR = (255, 0, 0)
PATH_COLOR = (180, 180, 180)  # 通道颜色

# 墙体模型数据
WALL_MODEL = {
    'base_color': (80, 80, 90),  # 金属灰底
    'center_highlight': (120, 120, 135),  # 中心高光
    'edge_highlight': (140, 140, 155),  # 边缘高光
    'edge_shadow': (50, 50, 60),  # 边缘阴影
    'corner_highlight': (150, 150, 180),  # 四角高光
    'corner_shadow': (100, 100, 110),  # 四角阴影
    'diagonal_color': (220, 220, 240, 25),  # 内部对角线颜色
    'polygon_colors': {
        'top_left': (170, 170, 190, 110),  # 左上（最亮）
        'top_right': (120, 120, 150, 90),  # 右上（次亮）
        'bottom_left': (70, 70, 100, 70),  # 左下（较暗）
        'bottom_right': (40, 40, 60, 50)   # 右下（最暗）
    }
}

# 箱子模型数据
BOX_MODEL = {
    'base_color': BOX_COLOR,
    'border_color': (220, 220, 220),
    'mark_color': (139, 69, 19),  # X标记颜色
    'target_mark_color': (255, 0, 0),  # 目标点上的标记颜色
    'padding': 2,  # 箱子边距
    'mark_padding': 5  # X标记边距
}

# 玩家模型数据
PLAYER_MODEL = {
    'head_color': (255, 223, 0),  # 头部颜色（金色）
    'body_color': PLAYER_COLOR,  # 身体颜色（红色）
    'head_radius': 8,
    'body_width': 3,
    'arm_width': 3,
    'leg_width': 3,
    'head_offset': -8,  # 头部Y偏移
    'body_start': 2,    # 身体开始Y偏移
    'body_end': 12,     # 身体结束Y偏移
    'arm_offset': -2,   # 手臂Y偏移
    'arm_width_pixels': 6,  # 手臂宽度像素
    'leg_start': 12,    # 腿部开始Y偏移
    'leg_end': 20,      # 腿部结束Y偏移
    'leg_width_pixels': 3   # 腿部宽度像素
}

# 路径模型数据
PATH_MODEL = {
    'light_color': (180, 220, 230),  # 浅蓝绿
    'dark_color': (150, 200, 210),   # 深蓝绿
    'border_color': (230, 245, 250)  # 边框颜色
}

# 目标点模型数据
TARGET_MODEL = {
    'color': TARGET_COLOR,
    'border_color': (220, 220, 220),
    'border_width': 2
}

def draw_wall(screen, x, y, tile_size):
    """绘制墙体"""
    rect = (x * tile_size, y * tile_size, tile_size, tile_size)
    
    # 底色
    pygame.draw.rect(screen, WALL_MODEL['base_color'], rect)
    
    # 中心高光
    inner_rect = (x * tile_size + 6, y * tile_size + 6, tile_size - 12, tile_size - 12)
    pygame.draw.rect(screen, WALL_MODEL['center_highlight'], inner_rect)
    
    # 边缘高光
    pygame.draw.line(screen, WALL_MODEL['edge_highlight'], (rect[0], rect[1]), (rect[0] + tile_size, rect[1]), 1)  # 上
    pygame.draw.line(screen, WALL_MODEL['edge_highlight'], (rect[0], rect[1]), (rect[0], rect[1] + tile_size), 1)  # 左
    
    # 边缘阴影
    pygame.draw.line(screen, WALL_MODEL['edge_shadow'], (rect[0], rect[1] + tile_size - 1), (rect[0] + tile_size, rect[1] + tile_size - 1), 1)  # 下
    pygame.draw.line(screen, WALL_MODEL['edge_shadow'], (rect[0] + tile_size - 1, rect[1]), (rect[0] + tile_size - 1, rect[1] + tile_size), 1)  # 右
    
    # 四角高光点
    pygame.draw.circle(screen, WALL_MODEL['corner_highlight'], (rect[0] + 4, rect[1] + 4), 1)
    pygame.draw.circle(screen, WALL_MODEL['corner_highlight'], (rect[0] + tile_size - 4, rect[1] + 4), 1)
    pygame.draw.circle(screen, WALL_MODEL['corner_shadow'], (rect[0] + 4, rect[1] + tile_size - 4), 1)
    pygame.draw.circle(screen, WALL_MODEL['corner_shadow'], (rect[0] + tile_size - 4, rect[1] + tile_size - 4), 1)
    
    # 内部分区光影（根据对角线分割，四区不同亮度，模拟立体光影）
    poly_surf = pygame.Surface((tile_size, tile_size), pygame.SRCALPHA)
    
    # 左上（最亮，主光源）
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['top_left'], [(0,0), (tile_size//2, tile_size//2), (0, tile_size)])
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['top_left'], [(0,0), (tile_size//2, tile_size//2), (tile_size,0)])
    
    # 右上（次亮，受光）
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['top_right'], [(tile_size,0), (tile_size//2, tile_size//2), (tile_size, tile_size)])
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['top_right'], [(tile_size,0), (tile_size//2, tile_size//2), (0,0)])
    
    # 左下（较暗，背光）
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['bottom_left'], [(0,tile_size), (tile_size//2, tile_size//2), (tile_size, tile_size)])
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['bottom_left'], [(0,tile_size), (tile_size//2, tile_size//2), (0,0)])
    
    # 右下（最暗，阴影）
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['bottom_right'], [(tile_size, tile_size), (tile_size//2, tile_size//2), (tile_size,0)])
    pygame.draw.polygon(poly_surf, WALL_MODEL['polygon_colors']['bottom_right'], [(tile_size, tile_size), (tile_size//2, tile_size//2), (0,tile_size)])
    
    screen.blit(poly_surf, (x * tile_size, y * tile_size))
    
    # 内部对角线（半透明银白色）
    diag_surf = pygame.Surface((tile_size, tile_size), pygame.SRCALPHA)
    pygame.draw.line(diag_surf, WALL_MODEL['diagonal_color'], (8, 8), (tile_size - 8, tile_size - 8), 2)
    pygame.draw.line(diag_surf, WALL_MODEL['diagonal_color'], (tile_size - 8, 8), (8, tile_size - 8), 2)
    screen.blit(diag_surf, (x * tile_size, y * tile_size))

def draw_target(screen, x, y, tile_size):
    """绘制目标点"""
    rect = (x * tile_size, y * tile_size, tile_size, tile_size)
    pygame.draw.rect(screen, TARGET_MODEL['color'], rect)
    pygame.draw.rect(screen, TARGET_MODEL['border_color'], rect, TARGET_MODEL['border_width'])

def draw_path(screen, x, y, tile_size):
    """绘制路径"""
    rect = (x * tile_size, y * tile_size, tile_size, tile_size)
    
    # 蓝绿色交替色彩
    if (x + y) % 2 == 0:
        path_color = PATH_MODEL['light_color']  # 浅蓝绿
    else:
        path_color = PATH_MODEL['dark_color']   # 深蓝绿
    
    pygame.draw.rect(screen, path_color, rect)
    # 增加浅灰色边框增强网格感
    pygame.draw.rect(screen, PATH_MODEL['border_color'], rect, 1)

def draw_player(screen, x, y, tile_size, on_target=False):
    """绘制玩家"""
    rect = (x * tile_size, y * tile_size, tile_size, tile_size)
    
    # 如果不在目标点上，先绘制通道底色
    if not on_target:
        if (x + y) % 2 == 0:
            path_color = PATH_MODEL['light_color']
        else:
            path_color = PATH_MODEL['dark_color']
        pygame.draw.rect(screen, path_color, rect)
    else:
        # 在目标点上，绘制目标点底色
        pygame.draw.rect(screen, TARGET_MODEL['color'], rect)
    
    # 绘制玩家（简单的像素风格人物）
    center_x, center_y = x * tile_size + tile_size // 2, y * tile_size + tile_size // 2
    
    # 头部
    pygame.draw.circle(screen, PLAYER_MODEL['head_color'], 
                      (center_x, center_y + PLAYER_MODEL['head_offset']), 
                      PLAYER_MODEL['head_radius'])
    
    # 身体
    pygame.draw.line(screen, PLAYER_MODEL['body_color'], 
                    (center_x, center_y + PLAYER_MODEL['body_start']), 
                    (center_x, center_y + PLAYER_MODEL['body_end']), 
                    PLAYER_MODEL['body_width'])
    
    # 手臂
    pygame.draw.line(screen, PLAYER_MODEL['body_color'], 
                    (center_x - PLAYER_MODEL['arm_width_pixels'], center_y + PLAYER_MODEL['arm_offset']), 
                    (center_x + PLAYER_MODEL['arm_width_pixels'], center_y + PLAYER_MODEL['arm_offset']), 
                    PLAYER_MODEL['arm_width'])
    
    # 腿
    pygame.draw.line(screen, PLAYER_MODEL['body_color'], 
                    (center_x - PLAYER_MODEL['leg_width_pixels'], center_y + PLAYER_MODEL['leg_start']), 
                    (center_x - PLAYER_MODEL['leg_width_pixels'], center_y + PLAYER_MODEL['leg_end']), 
                    PLAYER_MODEL['leg_width'])
    pygame.draw.line(screen, PLAYER_MODEL['body_color'], 
                    (center_x + PLAYER_MODEL['leg_width_pixels'], center_y + PLAYER_MODEL['leg_start']), 
                    (center_x + PLAYER_MODEL['leg_width_pixels'], center_y + PLAYER_MODEL['leg_end']), 
                    PLAYER_MODEL['leg_width'])

def draw_box(screen, x, y, tile_size, on_target=False):
    """绘制箱子"""
    rect = (x * tile_size, y * tile_size, tile_size, tile_size)
    
    if on_target:
        # 在目标点上，先绘制目标点
        pygame.draw.rect(screen, TARGET_MODEL['color'], rect)
    
    # 绘制箱子（带高光的棕色矩形）
    box_rect = (x * tile_size + BOX_MODEL['padding'], 
                y * tile_size + BOX_MODEL['padding'], 
                tile_size - BOX_MODEL['padding'] * 2, 
                tile_size - BOX_MODEL['padding'] * 2)
    pygame.draw.rect(screen, BOX_MODEL['base_color'], box_rect)
    
    # 添加箱子的"X"标记
    pygame.draw.line(screen, BOX_MODEL['mark_color'], 
                    (x * tile_size + BOX_MODEL['mark_padding'], y * tile_size + BOX_MODEL['mark_padding']),
                    (x * tile_size + tile_size - BOX_MODEL['mark_padding'], y * tile_size + tile_size - BOX_MODEL['mark_padding']), 3)
    pygame.draw.line(screen, BOX_MODEL['mark_color'], 
                    (x * tile_size + tile_size - BOX_MODEL['mark_padding'], y * tile_size + BOX_MODEL['mark_padding']),
                    (x * tile_size + BOX_MODEL['mark_padding'], y * tile_size + tile_size - BOX_MODEL['mark_padding']), 3)
    
    # 箱子边框
    pygame.draw.rect(screen, BOX_MODEL['border_color'], rect, 2)
    
    if on_target:
        # 目标点上的箱子标记
        center_x, center_y = x * tile_size + tile_size // 2, y * tile_size + tile_size // 2
        pygame.draw.circle(screen, BOX_MODEL['target_mark_color'], (center_x, center_y), 8, 0)

def draw_game_element(screen, element_type, x, y, tile_size):
    """根据元素类型绘制游戏元素"""
    if element_type == WALL:
        draw_wall(screen, x, y, tile_size)
    elif element_type == TARGET:
        draw_target(screen, x, y, tile_size)
    elif element_type == PATH:
        draw_path(screen, x, y, tile_size)
    elif element_type == PLAYER:
        draw_player(screen, x, y, tile_size, on_target=False)
    elif element_type == PLAYER_ON_TARGET:
        draw_player(screen, x, y, tile_size, on_target=True)
    elif element_type == BOX:
        draw_box(screen, x, y, tile_size, on_target=False)
    elif element_type == BOX_ON_TARGET:
        draw_box(screen, x, y, tile_size, on_target=True)

def get_element_name(element_type):
    """获取元素类型名称"""
    element_names = {
        PATH: "路径",
        WALL: "墙体",
        BOX: "箱子",
        TARGET: "目标点",
        PLAYER: "玩家",
        BOX_ON_TARGET: "目标点上的箱子",
        PLAYER_ON_TARGET: "目标点上的玩家"
    }
    return element_names.get(element_type, "未知元素")

def is_movable(element_type):
    """判断元素是否可移动"""
    return element_type in [PATH, TARGET]

def is_pushable(element_type):
    """判断元素是否可推动"""
    return element_type in [BOX, BOX_ON_TARGET]

def is_blocking(element_type):
    """判断元素是否阻挡移动"""
    return element_type == WALL

def is_target(element_type):
    """判断元素是否是目标点"""
    return element_type in [TARGET, BOX_ON_TARGET, PLAYER_ON_TARGET]
